package com.ruicar.afs.cloud.apply.contract.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.aggregate.dto.CertNoDto;
import com.ruicar.afs.cloud.aggregate.dto.VinCheckDto;
import com.ruicar.afs.cloud.apply.common.entity.ApplyContractInfo;
import com.ruicar.afs.cloud.apply.common.entity.ApplyCustBaseInfo;
import com.ruicar.afs.cloud.apply.common.entity.ApplyOrderInfo;
import com.ruicar.afs.cloud.apply.common.utils.ApplyConstants;
import com.ruicar.afs.cloud.apply.common.utils.ApplyUtils;
import com.ruicar.afs.cloud.apply.contract.service.ApplyContractInfoService;
import com.ruicar.afs.cloud.apply.contract.vo.*;
import com.ruicar.afs.cloud.apply.pre.loan.service.ApplyCustBaseInfoService;
import com.ruicar.afs.cloud.apply.pre.loan.service.ApplyOrderInfoService;
import com.ruicar.afs.cloud.bizcommon.business.entity.FinCostDetails;
import com.ruicar.afs.cloud.bizcommon.business.service.ApplyCostDetailsService;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: panhj
 * @date: 2020/9/14 19:39
 * @description: 聚合平台controller
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/get")
public class PolyMerizationController {
    private ApplyCustBaseInfoService applyCustBaseInfoService;
    private ApplyOrderInfoService applyOrderInfoService;
    private ApplyContractInfoService applyContractInfoService;
    private ApplyCostDetailsService applyCostDetailsService;
    @GetMapping("/checkcertnorepeat")
    @ApiOperation(value = "身份证验重")
    public JSONArray checkcertnorepeat(@ModelAttribute CertNoDto certNoDto) {
        List certNo=new ArrayList();
        JSONArray jsonArray=new JSONArray();
        if(certNoDto.getBorrowerCertno()!=null && !("").equals(certNoDto.getBorrowerCertno())){
            certNo.add(certNoDto.getBorrowerCertno());
        }
        if(certNoDto.getCoborrowerCertno()!=null && !("").equals(certNoDto.getCoborrowerCertno())){
            certNo.add(certNoDto.getCoborrowerCertno());
        }
        if(certNoDto.getGuarantorCertno()!=null && !("").equals(certNoDto.getGuarantorCertno())){
            certNo.add(certNoDto.getGuarantorCertno());
        }
        List<ApplyLoanAmtVo> applyLoanAmtVos=applyCustBaseInfoService.getApplyLoanAmt(certNo);
        for(ApplyLoanAmtVo applyLoanAmtVo:applyLoanAmtVos){
            applyLoanAmtVo.setStatusName(ApplyUtils.dicData(applyLoanAmtVo.getStatus(),"applyStatus"));
        }
        jsonArray.addAll(applyLoanAmtVos);
        return jsonArray;
    }

    @GetMapping("/usedcarhistory/nopayoffandapproval")
    @ApiOperation(value = "二手车历史存量vin查询")
    public JSONArray nopayoffandapproval(@RequestParam String vin){
        JSONArray jsonArray=new JSONArray();
        List<ApplyUseCarVinVo> applyUseCarVinVos=applyOrderInfoService.getOrderInfoByVin(vin);
        if(applyUseCarVinVos!=null){
            for(ApplyUseCarVinVo applyUseCarVinVo:applyUseCarVinVos){
                applyUseCarVinVo.setStatusName(ApplyUtils.dicData(applyUseCarVinVo.getStatus(),"applyStatus"));
            }
        }
        jsonArray.addAll(applyUseCarVinVos);
        return jsonArray;
    }
    @GetMapping("/activedcontractlist")
    @ApiOperation(value = "获取指定数量的某公司激活合同列表")
    public JSONArray activedcontractlist(@RequestParam String creditCode,@RequestParam Integer limitedNumber){
        JSONArray jsonArray=new JSONArray();
        List<ApplyActiveContractVo> applyActiveContractVoList=null;
        if(creditCode!=null && !("").equals(creditCode)){
            applyActiveContractVoList=applyContractInfoService.getContractInfoByCreditCode(creditCode);
        }
        if(applyActiveContractVoList!=null){
            if(applyActiveContractVoList.size()>=limitedNumber){
                applyActiveContractVoList= applyActiveContractVoList.subList(0,limitedNumber);
            }
        }
        jsonArray.addAll(applyActiveContractVoList);
        return jsonArray;
    }
    @GetMapping("/allaom")
    @ApiOperation(value = "allaom")
    public JSONObject allaom(@RequestParam String creditCode, @RequestParam String criticalTime){
        JSONObject jsonObject=new JSONObject();
        JSONArray jsonArray=new JSONArray();
        List<ApplyAllAomVo> applyAllAomVoList=applyContractInfoService.getContractAllAom(creditCode,criticalTime);
        if(applyAllAomVoList!=null){
            for(ApplyAllAomVo applyAllAomVo:applyAllAomVoList){
                applyAllAomVo.setStatusName(ApplyUtils.dicData(applyAllAomVo.getStatus(),"contractStatus"));
                List<FinCostDetails> finCostDetailsList =applyCostDetailsService.getCostDetailsList(applyAllAomVo.getApplicationNo());
                BigDecimal loanAmt=BigDecimal.ZERO;
                for(FinCostDetails finCostDetails:finCostDetailsList){
                    loanAmt=loanAmt.add(finCostDetails.getLoanAmt());
                }
                applyAllAomVo.setLoanMoney(loanAmt);
                applyAllAomVo.setRepaidMoney(loanAmt);
            }
        }
        List<ApplyContractInfo> applyContractInfoList=applyContractInfoService.getContractInfoByAom(creditCode,criticalTime);
        Integer count=0;
        if(applyContractInfoList!=null){
            count=applyContractInfoList.size();
        }
        jsonArray.addAll(applyAllAomVoList);
        jsonObject.put("data",jsonArray);
        jsonObject.put("waittingApproval",count);
        return jsonObject;
    }
    @GetMapping("/checkvinrepeat")
    @ApiOperation(value = "放款vin查重")
    public JSONArray checkvinrepeat(@RequestParam String vin){
        JSONArray jsonArray=new JSONArray();
        List<ApplyContractVinVo> applyContractVinVos=applyContractInfoService.getContractInfoByVin(vin);
        if(applyContractVinVos.size()>0){
            for(ApplyContractVinVo applyContractVinVo:applyContractVinVos){
                ApplyCustBaseInfo applyCustBaseInfo=applyCustBaseInfoService.getCustBaseInfo(applyContractVinVo.getApplicationNo(), ApplyConstants.PRINCIPAL_BORROWER);
                applyContractVinVo.setCertNo(applyCustBaseInfo.getCertNo());
            }
        }
        jsonArray.addAll(applyContractVinVos);
        return jsonArray;
    }
}
